package putao.ui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Keyboard;
	
	import mx.managers.FocusManager;
	
	import putao.ui.assets.Assets;
	import putao.ui.interf.IFocusComponent;
	
	/**
	 * 最基本的按钮，有普通，移上，按下，无效四种状态
	 *  
	 * @author putao
	 * 
	 */	
	public class PButton extends Sprite implements IFocusComponent
	{
		private var _focusEnabled:Boolean = true;
		
		//按钮的四种状态
		protected static const BTN_UP:String = "BTN_UP";
		protected static const BTN_OVER:String = "BTN_OVER";
		protected static const BTN_PRESSED:String = "BTN_PRESSED";
		protected static const BTN_DISABLED:String = "BTN_DISABLED";
		
		protected var currentState:String = "";
		//按钮背景
		protected var sBtnDafult:DisplayObject;
		protected var sBtnRollover:DisplayObject;
		protected var sBtnPressed:DisplayObject;
		protected var sBtnDisabled:DisplayObject;
		//舞台
		protected var thisStage:Stage;
		//当前显示的背景图
		protected var currentDisObj:DisplayObject;
		//鼠标是否在按钮上方
		protected var isOver:Boolean;
		//按钮是否按下
		protected var isDown:Boolean;
		
		/**
		 * 显示文本 
		 */		
		public var txtLabel:TextField;
		
		public function PButton(text:String = "")
		{
			super();
			initDefaultSkin();
			
			currentDisObj = sBtnDafult;
			this.addChildAt(currentDisObj, 0);
			txtLabel = new TextField();
			this.text = text;
			txtLabel.selectable = false;
			txtLabel.mouseEnabled = false;
			txtLabel.width = currentDisObj.width;
			txtLabel.height = 20;
			txtLabel.autoSize = TextFieldAutoSize.CENTER;
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			this.mouseChildren = false;
			this.buttonMode = true;
		}
		
		/**
		 *
		 * 设置文本内容
		 *  
		 * @param value
		 * 
		 */		
		public function set text(value:String):void
		{
			txtLabel.text = value;
			if(value == ""){
				if(this.contains(txtLabel)){
					this.removeChild(txtLabel);
				}
			}else{
				this.addChild(txtLabel);
			}
		}
		
		public function get text():String
		{
			return txtLabel.text;
		}
		
		/**
		 * 设置按钮的大小，用这个方法才能保证按钮不变形
		 *   
		 * @param width 宽度
		 * @param height 高度
		 * 
		 */		
		public function resize(width:Number, height:Number):void
		{
			var arr:Array = [sBtnDafult, sBtnRollover, sBtnPressed, sBtnDisabled];
			for(var i:int = 0; i < arr.length; i++){
				arr[i].width = width;
				arr[i].height = height;
			}
			txtLabel.x = width / 2 - txtLabel.width / 2;
			txtLabel.y = height / 2 - txtLabel.height / 2;
		}
		
		/**
		 *
		 * 设置按钮的皮肤 
		 * 
		 * @param btnDafult 默认状态
		 * @param btnRollover 鼠标移上
		 * @param btnPressed 按下
		 * @param btnDisabled 无效
		 * 
		 */		
		public function setSkin(btnDafult:DisplayObject, btnRollover:DisplayObject, btnPressed:DisplayObject, btnDisabled:DisplayObject):void
		{
			sBtnDafult = btnDafult;
			sBtnRollover = btnRollover;
			sBtnPressed = btnPressed;
			sBtnDisabled = btnDisabled;
//			currentDisObj = getDisObj(currentState);
			changeShowImg(currentState);
		}
		
		/**
		 *
		 * 初始化按钮默认皮肤 
		 * 
		 */		
		protected function initDefaultSkin():void
		{
			sBtnDafult = Assets.getDisplayObject(Assets.BtnDefault);
			sBtnRollover = Assets.getDisplayObject(Assets.BtnRollover);
			sBtnPressed = Assets.getDisplayObject(Assets.BtnPressed);
			sBtnDisabled = Assets.getDisplayObject(Assets.BtnDisabled);
		}
		
		public function onFocusHandler():void
		{
			FocusShape.getInstance().setRect( -1, -1, currentDisObj.width + 2, currentDisObj.height + 2);
			this.addChild(FocusShape.getInstance());
		}
		
		public function unFocusHandler():void
		{
			
		}
		
		public function doSpaceDown():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
		}
		
		public function doSpaceUp():void
		{
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		
		protected function onMouse(e:MouseEvent):void
		{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					isDown = true;
					changeShowImg(BTN_PRESSED);
					break;
				case MouseEvent.MOUSE_UP:
					isDown = false;
					if(isOver){
						changeShowImg(BTN_OVER);
					}else{
						changeShowImg(BTN_UP);
					}
					break;
				case MouseEvent.ROLL_OUT:
					isOver = false;
					if(isDown){
						changeShowImg(BTN_OVER);
					}else{
						changeShowImg(BTN_UP);
					}
					break;
				case MouseEvent.ROLL_OVER:
					isOver = true;
					if(isDown){
						changeShowImg(BTN_PRESSED);
					}else{
						changeShowImg(BTN_OVER);
					}
					break;
			}
		}
		
		//改变当前显示的按钮背景图,
		protected function changeShowImg(state:String):void
		{
//			if(state == currentState)
//				return;
			this.currentState = state;
			this.removeChild(currentDisObj);
			currentDisObj = getDisObj(state);
			this.addChildAt(currentDisObj, 0);
		}
		
		protected function onAddToStage(e:Event):void
		{
			thisStage = this.stage;
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			this.addEventListener(MouseEvent.ROLL_OVER, onMouse, false, 0, true);
			this.addEventListener(MouseEvent.ROLL_OUT, onMouse, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouse, false, 0, true);
			thisStage.addEventListener(MouseEvent.MOUSE_UP, onMouse, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemove, false, 0, true);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddAgain, false, 0, true);
			
			PFocusManager.getInstance(stage).addFocusObject(this);
		}
		
		/**
		 *
		 * 从舞台上移除是，去除焦点管理器中的引用
		 *  
		 * @param e
		 * 
		 */		
		protected function onRemove(e:Event):void
		{
			PFocusManager.getInstance().deleteFocusObject(this);
		}
		
		/**
		 *
		 * 再次添加到舞台上时，添加到焦点管理器中
		 *  
		 * @param e
		 * 
		 */		
		protected function onAddAgain(e:Event):void
		{
			PFocusManager.getInstance().addFocusObject(this);
		}
		
		protected function getDisObj(state:String):DisplayObject
		{
			switch(state)
			{
				case BTN_UP:
					return sBtnDafult;
					break;
				case BTN_OVER:
					return sBtnRollover;
					break;
				case BTN_PRESSED:
					return sBtnPressed;
					break;
				case BTN_DISABLED:
					return sBtnDisabled;
					break;
			}
			return sBtnDafult;
		}
	}
}